/* 
 * File:   BoundingBox.cpp
 * Author: brady
 * 
 * Created on March 10, 2011, 3:23 PM
 */

#include "BoundingBox.h"

BoundingBox::BoundingBox() {
     xMin = xMax = yMin = yMax = zMin = zMax = 0;
}

BoundingBox::BoundingBox(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax)
{
    xMin = xmin - 2.0*FLT_EPSILON;
    xMax = xmax + 2.0*FLT_EPSILON;
    yMin = ymin - 2.0*FLT_EPSILON;
    yMax = ymax + 2.0*FLT_EPSILON;
    zMin = zmin - 2.0*FLT_EPSILON;
    zMax = zmax + 2.0*FLT_EPSILON;

}

BoundingBox::~BoundingBox() {
}

void BoundingBox::Absorb(BoundingBox* bb)
{
    this->xMin = min(this->xMin, bb->xMin)- 2.0*FLT_EPSILON;
    this->xMax = max(this->xMax, bb->xMax)+ 2.0*FLT_EPSILON;
    this->yMin = min(this->yMin, bb->yMin)- 2.0*FLT_EPSILON;
    this->yMax = max(this->yMax, bb->yMax)+ 2.0*FLT_EPSILON;
    this->zMin = min(this->zMin, bb->zMin)- 2.0*FLT_EPSILON;
    this->zMax = max(this->zMax, bb->zMax)+ 2.0*FLT_EPSILON;
}

void BoundingBox::Transform(TransformMatrix t)
{
    vector<Vector3D> points;
    Vector3D A, B, C, D, E, F, G, H;
    A = Vector3D(xMin, yMin, zMin);
    B = Vector3D(xMax, yMin, zMin);
    C = Vector3D(xMin, yMin, zMax);
    D = Vector3D(xMax, yMin, zMax);
    E = Vector3D(xMin, yMax, zMin);
    F = Vector3D(xMax, yMax, zMin);
    G = Vector3D(xMin, yMax, zMax);
    H = Vector3D(xMax, yMax, zMax);


    points.push_back(A);
    points.push_back(B);
    points.push_back(C);
    points.push_back(D);
    points.push_back(E);
    points.push_back(F);
    points.push_back(G);
    points.push_back(H);

    xMin = DBL_MAX;
    yMin = DBL_MAX;
    zMin = DBL_MAX;
    xMax = -DBL_MAX;
    yMax = -DBL_MAX;
    zMax = -DBL_MAX;

    for(int i = 0; i<points.size(); i++)
    {
        Vector3D pt = points[i];
        pt = t.Transform(pt);
        points[i] = pt;
    }

    for(int i = 0; i<points.size(); i++)
    {
        xMin = min(xMin, points[i][0]);
        xMax = max(xMax, points[i][0]);
        yMin = min(yMin, points[i][1]);
        yMax = max(yMax, points[i][1]);
        zMin = min(zMin, points[i][2]);
        zMax = max(zMax, points[i][2]);
    }
}